﻿
package
	{
	import flash.display.MovieClip;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	
	import flash.display.DisplayObject;
	import flash.geom.*;
	
	import flash.display.*;
	import flash.net.URLRequest;
	import flash.events.*;
	
	public class Map extends MovieClip
		{
		var loadCount:Number = 0;
		var loadedBitmap:Array = new Array();
		var loadingList:Array = new Array("mask.png", "macadam.gif","water4.gif", "gradientmask.png");
		var loader:Loader = new Loader();
		var urlReq:URLRequest;
		
		var bitData:Grass = new Grass(400,400);
		//var maskbitmap:Mask = new Mask(400,400);
		
		var bitmap:BitmapData = new BitmapData(1600,1200, false, 0x00ff00);
		var dPt:Point = new Point(0,0);
		var sRect:Rectangle = new Rectangle(0,0,1600,1200);
		var colorFilter = new ColorTransform();
		
		public function Map()
			{
			//loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
			urlReq = new URLRequest(loadingList[loadCount]);
			loader.load(urlReq);
			
			//bitData = BitmapData(tempbitmap.bitmapData);
			
			/*colorFilter.redOffset = 20;
			colorFilter.greenOffset = 90;
			colorFilter.blueOffset = 20;
			
			bitData.perlinNoise(15,15, 1, Math.random()*100, true, false, 5, true);
			bitmap.perlinNoise(5, 20, 2, Math.random()*1000, false, true, 3, true);
			bitmap.colorTransform(sRect, colorFilter);
			
			var zPt:Point = new Point(0,0);
			
			for (var i=0; i< 10; i++)
				{
				dPt.y = i*150;
				
				for (var j=0; j< 10; j++)
					{
					dPt.x = j*120;
					if (Math.random() > 0.75)
						bitmap.copyPixels(bitData, sRect, dPt, maskbitmap, zPt);
					}
				
				}*/
			}
			
		 function completeHandler(event:Event):void
		 	{
			loadedBitmap.push(Bitmap(loader.content));
			
			if (loadedBitmap.length == loadingList.length)
				createMap();
			else
				{
				urlReq.url = loadingList[++loadCount];
				loader.load(urlReq);
				}
			}
		
		function createMap()
			{
			var zPt:Point = new Point(0,0);
			
			for (var i=0; i< 3; i++)
				{
				dPt.y = i*400 ;
				
				for (var j=0; j< 4; j++)
					{
					dPt.x = j*400;
					
					if (j==0) // grass
						bitmap.copyPixels(bitData , sRect, dPt, null, zPt);
					else if (j==1) // macadam
						{
						bitmap.copyPixels(loadedBitmap[1].bitmapData , sRect, dPt, null, zPt);
						bitmap.copyPixels(bitData , sRect, dPt, loadedBitmap[0].bitmapData, zPt);
						}
					else if (j==2)
						{
						bitmap.copyPixels(loadedBitmap[1].bitmapData , sRect, dPt, null, zPt);
						bitmap.copyPixels(loadedBitmap[2].bitmapData , sRect, dPt, loadedBitmap[3].bitmapData, zPt);
						}
					else if (j==3)
						bitmap.copyPixels(loadedBitmap[2].bitmapData , sRect, dPt, null, zPt);
						
					}
				
				}
			
			var bit:Bitmap = new Bitmap(bitmap);
			this.addChildAt (bit,0);
			}
		
		}
		
	}